package com.meet.controller;

import com.meet.dao.*;
import com.meet.entity.BoardRoom;
import com.meet.entity.Meeting;
import com.meet.entity.PageBean;
import com.meet.entity.Workers;
import com.meet.service.*;
import com.meet.util.DateUtil;
import com.meet.vo.MeetingConfirm;
import com.meet.vo.MeetingVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@WebServlet("/servlet/MeetingServlet")
public class MeetingServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MeetingDao meetingDao = new MeetingDaoImpl();
        MeetingService meetingService = new MeetingServiceImpl(meetingDao);
        BoardRoomDao boardRoomDao =new BoardRoomDaoImpl();
        BoardRoomService boardRoomService=new BoardRoomServiceImpl(boardRoomDao);
        String flag = request.getParameter("flag");
        WMDao wmDao=new WMDaoImpl();
        WMService wmService=new WMServiceImpl(wmDao);
        WorkersDao workersDao=new WorkersDaoImpl();
        WorkersService workersService=new WorkersServiceImpl(workersDao);

        /**
         * flag=1&page=1 查所有 ；
         * flag=2增加
         * flag=3&id=1 跳修改页面；
         * flag=4 提交修改；
         * flag=5&id=1 删除
         */

        if ("1".equals(flag)) {

            String currentPage = request.getParameter("page");//当前页码
            String rows = request.getParameter("rows");//每页显示条数

            if (currentPage == null || "".equals(currentPage)) {

                currentPage = "1";
            }

            if (rows == null || "".equals(rows)) {
                rows = "4";
            }
            PageBean<MeetingVo> pb = null;

            try {
                pb = meetingService.getAllMeetsByPage(currentPage, rows);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            //3.将PageBean存入request
            request.setAttribute("pb", pb);

            //4.转发到main.jsp
            request.getRequestDispatcher("meetingShow.jsp").forward(request, response);


        } else if ("2".equals(flag)) {

            String mName = request.getParameter("mName");
            Integer rId = Integer.valueOf(request.getParameter("rId"));
            String start =request.getParameter("tStart");
            System.out.println(start);
            start=start.replace('T',' ')+":00";
            System.out.println(start);
            String end =request.getParameter("tEnd");
            System.out.println(start);
            end=end.replace('T',' ')+":00";
            System.out.println(end);
            Timestamp tStart=new Timestamp(DateUtil.string3Date(start).getTime());
            Timestamp tEnd=new Timestamp(DateUtil.string3Date(end).getTime());
//            Timestamp tStart=new Timestamp(DateUtil.string3Date(request.getParameter("tStart")).getTime());
//            Timestamp tEnd=new Timestamp(DateUtil.string3Date(request.getParameter("tEnd")).getTime());
            Integer wId = Integer.valueOf(request.getParameter("wId"));
            Timestamp tReserve =null;
            String content = request.getParameter("content");

            Meeting meeting = new Meeting(null, mName, rId, tStart, tEnd, wId, tReserve, content);

            try {
              int mymid=  meetingService.addMeeting(meeting);
                // int mymid=1;
            //------------
            String[] ids = request.getParameterValues("work");
            System.out.println("拿到了ids");
            if(ids != null){
                for(int i=0;i<ids.length;i++){
                    int myuserid=Integer.parseInt(ids[i]);
                    //System.out.println("+++ "+i+"---"+myuserid);
                    //在这里插入连接表
                    wmService.Addamessage(myuserid,mymid);
                }

            }
            //------------
            } catch (SQLException e) {
                e.printStackTrace();
            }

            request.getRequestDispatcher("MeetingServlet?flag=1").forward(request, response);

        } else if ("3".equals(flag)) {

            Integer id = Integer.valueOf(request.getParameter("id"));

            try {

                MeetingVo meeting = meetingService.getMeeting(id);
                List<BoardRoom> rooms=boardRoomService.getAllRooms(1,10000);

                request.setAttribute("meeting", meeting);
                request.setAttribute("rooms",rooms);
                request.getRequestDispatcher("meetingEdit.jsp").forward(request, response);

            } catch (SQLException e) {
                e.printStackTrace();
            }

        } else if ("4".equals(flag)) {
            System.out.println("---4");
            String mName = request.getParameter("mName");
            Integer id =Integer.parseInt(request.getParameter("id"));
            Integer rId = Integer.parseInt(request.getParameter("rId"));
            Timestamp tStart=new Timestamp(DateUtil.string3Date(request.getParameter("tStart")).getTime());
            Timestamp tEnd=new Timestamp(DateUtil.string3Date(request.getParameter("tEnd")).getTime());
            Integer wId = Integer.valueOf(request.getParameter("wId"));
            Timestamp tReserve =new Timestamp(new Date().getTime());
            String content = request.getParameter("content");

            Meeting meeting = new Meeting(id, mName, rId, tStart, tEnd, wId, tReserve, content);

            try {
                meetingService.updateMeeting(meeting);
            } catch (SQLException e) {
                e.printStackTrace();
            }

            request.getRequestDispatcher("MeetingServlet?flag=1").forward(request, response);

        } else if ("5".equals(flag)) {

            Integer id = Integer.parseInt(request.getParameter("id"));

            try {
                meetingService.deleteMeeting(id);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            request.getRequestDispatcher("MeetingServlet?flag=1").forward(request, response);
        }else if ("6".equals(flag)) {
            String roomid= request.getParameter("roomid");
            String starttime=request.getParameter("start");
            String endtime =request.getParameter("end");
            Integer max=Integer.parseInt(request.getParameter("maxnumber"));
          /*  System.out.println(roomid);
            System.out.println(starttime);
            System.out.println(endtime);*/
            request.setAttribute("roomid",roomid);
            request.setAttribute("starttime",starttime);
            request.setAttribute("endtime",endtime);
            request.setAttribute("max",max);
            try {
                List<BoardRoom> rooms=boardRoomService.getAllRooms(0,10000);
                request.setAttribute("rooms",rooms);
                List<Workers> workers=workersService.getAllWorkers();
                request.setAttribute("workers",workers);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            request.getRequestDispatcher("meetingAdd.jsp").forward(request,response);


        } else if("7".equals(flag)){
            //是否确认
            System.out.println("进入了7");
            String s = request.getParameter("tag");
            Integer mid = Integer.parseInt(request.getParameter("mid"));

            Boolean tag = false;
            if("1".equals(s)){
                tag = true;
            }else{
                tag = false;
            }
            System.out.println(tag);
            System.out.println("id"+mid);

            List<MeetingConfirm> meetConfirm = new ArrayList<MeetingConfirm>();

            try {
                meetConfirm = meetingService.getMeetingConfirm(tag,mid);
                System.out.println(meetConfirm);

                request.setAttribute("meetConfirm",meetConfirm);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            request.getRequestDispatcher("meetingConfirm.jsp").forward(request, response);
        }




    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
